figure_3 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_dislike_group) %>%
  filter(name %in% c("avg_score_dislike_group")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Ethnic Outgroup Animosity\n(Index, 1-5)") +
  coord_cartesian(ylim = c(2.45, 2.85))


figure_4 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_dislike_pol) %>%
  filter(name %in% c("avg_score_dislike_pol")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Political Outgroup Animosity\n(Index, 1-5)") +
  coord_cartesian(ylim = c(2.68, 3.08))


ggsave(filename = "./outputs/figures/figure_a3a.pdf", plot = figure_3, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_a3a.eps", device = "eps", plot = figure_3, width = 5, height = 4)

ggsave(filename = "./outputs/figures/figure_a3b.pdf", plot = figure_4, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_a3b.eps", device = "eps", plot = figure_4, width = 5, height = 4)
